<?php

namespace app\admin\controller;


use think\Controller;
use think\Db;
use think\Session;

class Index extends Authorized
{

    public function index(){return $this->fetch();}

    public function console()
    {
        return $this->fetch();
    }

    public function messages()
    {
        return $this->fetch();
    }


    public function setstate()
    {
        $post_data = $this->request->post();
        if($post_data){
            $result =Db::table('task')->where('id', $post_data['id'])->setField($post_data);
            if (false === $result) {
                return json(['msg' =>'失败', 'code' =>1]);
            } else {
                return json(['msg' =>'成功', 'code' =>0]);
            }
        }
    }

    public function change_pwd()
    {
        $data = $this->request->post();
        if($data){
            $res = Db::table('user')->where(['id' => Session::get('admin.id'), 'password'=>md5($data['oldPassword'])])->find();
            if ($res) {
                $result = Db::table('user')
                    ->where(['id' => Session::get('admin.id')])
                    ->setField('password', md5($data['password']));
                if ($result) {
                    return json(['msg' =>'修改成功', 'code' =>0]);
                }else{
                    return json(['msg' =>'修改失败', 'code' =>1]);
                }

            }else{
                return json(['msg' =>'旧密码错误', 'code' =>1]);
            }
        }else{
            return $this->fetch();
        }

    }




}